var util = require('../../utils/util.js');

// pages/register/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: true,
    curPhone: '',
    myCode:"",
    curPsw:"",
    isSend: false,
    btntext: '获取验证码',
    codev: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  savePhone: function (e) {
    var phone = e.detail.value;
    console.log(phone);
    this.setData({
      curPhone: phone
    })
    wx.setStorageSync('phone', phone)
  },

  findPhone() {
    if (this.data.isSend){
      return;
    }
    var that = this;
    if (this.data.curPhone.length == 11 && this.data.curPhone.indexOf('1') == 0){
      that.setData({
        isSend:true
      })
      
      let data = {
        mobile: this.data.curPhone,
        event: 'register'
      }
      util.http({
        url: '/api/miniwechat/user/sendSms',
        method: 'POST',
        data
      }).then(res => {
        console.log(res);
        var restTimeStamp = new Date().getTime() + 60 * 1000;
        wx.setStorageSync('REST_COUNT_DOWN', restTimeStamp);
        countDown_60s(that, restTimeStamp);
      }).catch(ex => { 
        wx.showToast({
          title: String(ex),
          icon: 'none',
          duration: 2000
        })
      })
    } else {
      wx.showToast({
        title: "请输入正确手机号！",
        icon: 'none',
        duration: 2000
      })
    }
  },

  inputCode(e) {
    this.setData({
      myCode: e.detail.value
    })
    console.log(this.data.myCode);
  },

  savePwd: function (e) {
    var pwd = e.detail.value;
    console.log(pwd);
    this.setData({
      curPsw: pwd
    })
    wx.setStorageSync('password', pwd)
  },

  register:function(){
    if (!this.data.checked){
      wx.showToast({
        title: "请勾选用户服务协议",
        icon: 'none',
        duration: 2000
      })
    }
    if (this.data.checked){
      let data = {
        mobile: this.data.curPhone,
        event: 'register',
        captcha: this.data.myCode,
        password: this.data.curPsw
      }
      util.http({
        url: '/api/miniwechat/user/register',
        method: 'POST',
        data: data
      }).then(res => {
        wx.reLaunch({
          url: '/pages/index/index'
        })
      }).catch(ex => {
        let param = {
          account: this.data.curPhone,
          password: this.data.curPsw
        }
        util.http({
          url: 'api/miniwechat/user/login',
          data: param,
          method: 'POST'
        }).then((data) => {
          wx.setStorage({
            key: 'userInfo',
            data: data.userinfo
          })
          wx.reLaunch({
            url: '/pages/index/index'
          })
        }).catch((err) => {
          wx.showToast({
            title: String(err),
            icon: 'none',
            duration: 2000
          })
        })
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onChange(event) {
    this.setData({
      checked: event.detail
    });
  }
})


function countDown_60s(_this, restTimeStamp) {
  var nowtimeStamp = new Date().getTime();
  var coden = parseInt((restTimeStamp - nowtimeStamp) / 1000); // 定义60秒的倒计时
  if (coden > 0) {
    _this.setData({
      isSend: true
    })
    _this.data.codeV = setInterval(function () {
      _this.setData({
        btntext: (--coden) + 's'
      })
      if (coden < 0) { // 清除setInterval倒计时，这里可以做很多操作，按钮变回原样等
        clearInterval(_this.data.codeV);
        wx.removeStorageSync('REST_COUNT_DOWN');
        _this.setData({
          isSend: false,
          btntext: '重新发送'
        })
      }
    }, 1000) //  1000是1秒
  } else {
    _this.setData({
      isSend: false
    })
  }
}